﻿using MTQ.BLL;
using MTQ.Common;
using MTQ.IBLL;
using MTQ.Model;
using MTQ.Model.PhoneModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;


namespace MTQ.Web.Controllers
{
    public class PhoneController :ApiController
    {
        //
        // GET: /Default1/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        //public ActionResult GetData()
        //{

        //    return Json("aaaa", JsonRequestBehavior.AllowGet);
        //}

        static List<string> listToken = new List<string>();
        //[System.Web.Http.HttpGet]
        //[System.Web.Http.HttpPost]
        public string Login(string username ,string password)
        {
            //var username = Request["username"].ToString();
            //var password = Request["password"].ToString();
            Phone_UserLogin userLogin = CheckUserInfo(username, password);
            return  "OK";//Json(userLogin,JsonRequestBehavior.AllowGet)
        }

        private IBase_UserInfoService _userInfoService = new Base_UserInfoService();
        public Phone_UserLogin CheckUserInfo(string UserName, string Password)//, string Code
        {
            // Sys_EventLogService.LogWrite("/LoginController/CheckUserInfo", this.Request, null, "登录", "使用账户" + UserName);
            Phone_UserLogin phone_UserLogin = new Phone_UserLogin();

            //调用业务逻辑层（BLL）去校验用户是否正确,,,定义变量存取获取到的用户的错误信息
            string UserInfoError = "";
            Base_UserInfo userInfoLogin = new Base_UserInfo();
            userInfoLogin.User_UserName = UserName;
            userInfoLogin.User_Password = MD5Cryptography.MD5Encrypt(Password);
            var loginUserInfo = _userInfoService.CheckUserInfo(userInfoLogin);

            switch (loginUserInfo)
            {
                case LoginResult.PwdError:
                    UserInfoError = "密码输入错误";
                    break;
                case LoginResult.UserNotExist:
                    UserInfoError = "用户名输入错误或者您已经被禁用";
                    break;
                case LoginResult.UserIsNull:
                    UserInfoError = "用户名不能为空";
                    break;
                case LoginResult.PwdIsNUll:
                    UserInfoError = "密码不能为空";
                    break;
                case LoginResult.OK:
                    UserInfoError = "OK";
                    break;
                case LoginResult.UserActivation:
                    UserInfoError = "用户名未激活";
                    break;
                default:
                    UserInfoError = "未知错误，请您检查您的数据库";
                    break;
            }

            //登录成功保存用户信息
            if (loginUserInfo == LoginResult.OK)
            {

                //保存登陆用户账号信息
                Base_UserInfo userInfoShow = _userInfoService.LoadEntities(p => p.User_UserName == UserName).FirstOrDefault();
                if (userInfoShow.User_UserName == "Admin")
                {
                    userInfoShow.IsSuperAdmin = true;
                }
                // Session["UserInfo"] = userInfoShow;
                //phone_UserLogin.Tokan = Phone_Info.AddUserinfo(userInfoShow, ProjectInfoService.GetUserControlProjectNumber(userInfoShow));

                Base_LocalUser localuser = new Base_LocalUserService().LoadEntities(x => x.User_Id == userInfoShow.User_Id).FirstOrDefault();
                if (localuser != null)
                {
                   // Session["CultureInfoName"] = localuser.LanguageName;
                }

                phone_UserLogin.Result = true;
                
            }
            phone_UserLogin.Message = UserInfoError;


            return phone_UserLogin;
        }

    }
}
